using System;
using System.Collections.Generic;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;
using Animoso.Domain;
using Animoso.Domain.Data;

namespace Animoso.Web.Controllers
{
    public class NewsController : ApplicationController
    {
        private readonly INewsItemRepository _newsRepository;
        private readonly IUserRepository _userRepository;

        public NewsController(INewsItemRepository newsRepository, IUserRepository userRepository)
        {
            _newsRepository = newsRepository;
            _userRepository = userRepository;
        }

        public ActionResult Index()
        {
            var newsItems = _newsRepository.GetAll();
            return View(newsItems);
        }

        [AcceptVerbs(HttpVerbs.Get)]
        public ActionResult Create()
        {
            NewsItem newsItem = new NewsItem();
            newsItem.DateTime = DateTime.Now;
            Domain.User user = _userRepository.GetByUsername(User.Identity.Name);
            newsItem.Author = user;
            newsItem.Published = false;

            _newsRepository.Add(newsItem);

            return View("Edit", newsItem);
        }

        [AcceptVerbs(HttpVerbs.Get)]
        public ActionResult Edit(int id)
        {
            NewsItem newsItem = _newsRepository.GetById(id);
            newsItem.Published = false;
            _newsRepository.Save(newsItem);
            return View(newsItem);
        }

        [ValidateInput(false)]
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Publish(NewsItem newsItem)
        {
            if (String.IsNullOrEmpty(newsItem.Title))
            {
                ModelState.AddModelError("Title", "Ongeldige Titel.");
                return View("Edit", newsItem);
            }

            if (String.IsNullOrEmpty(newsItem.Text))
            {
                ModelState.AddModelError("Text", "Ongeldige Text.");
                return View("Edit", newsItem);
            }

            if (ModelState.IsValid)
            {
                Domain.User user = _userRepository.GetByUsername(User.Identity.Name);
                if (user == null)
                {
                    ModelState.AddModelError("Author", "Je moet wel ingelogd zijn.");
                    return View("Edit", newsItem);
                }

                newsItem.Author = user;
                newsItem.DateTime = DateTime.Now;

                NewsItem oldNewsItem = _newsRepository.GetById(newsItem.Id);
                if(!string.IsNullOrEmpty(oldNewsItem.Picture))
                {
                    newsItem.Picture = oldNewsItem.Picture;
                }

                newsItem.Published = true;

                _newsRepository.Save(newsItem);
                return RedirectToAction("Index", "News");
            }
            return View("Edit", newsItem);
        }
        
        /*[AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Edit(NewsItem newsItem)
        {
            newsItem.DateTime = DateTime.Now;
            Domain.User user = _userRepository.GetByUsername(User.Identity.Name);
            newsItem.Author = user;
            if (!ModelState.IsValid)
                return View();

            _newsRepository.Save(newsItem);            
            return RedirectToAction("Index","News");
        }*/

        private string MapPath(string fileName)
        {
            return Server.MapPath("\\Upload\\" + Folders.NEWS_PICTURE_FOLDER) + "\\" + fileName;
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public string Upload(HttpPostedFileBase FileData, int newsItemId)
        {
            NewsItem newsItem = _newsRepository.GetById(newsItemId);

            if(!string.IsNullOrEmpty(newsItem.Picture))
            {
                System.IO.File.Delete(MapPath(newsItem.Picture));
            }

            Guid pictureGuid = Guid.NewGuid();
            string fileName = pictureGuid.ToString() + "." +
                               FileData.FileName.Split('.')[1];

            string pathToImg = MapPath(fileName);

            FileData.SaveAs(pathToImg);

            newsItem.Picture = fileName;
            _newsRepository.Save(newsItem);

            return fileName;
        }

        public JsonResult Delete(int newsItemId)
        {
            NewsItem newsItem = _newsRepository.GetById(newsItemId);
            if (!string.IsNullOrEmpty(newsItem.Picture))
            {
                System.IO.File.Delete(MapPath(newsItem.Picture));
            }
            _newsRepository.Delete(newsItem);

            return Json(true, JsonRequestBehavior.AllowGet);
        }
    }
}
